package phapp.action.system;

import java.io.*;
import java.util.*;

import javax.servlet.http.*;
import net.sf.json.JSONObject;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;
import javacommon.jquery.JqueryPageHelper;
import javacommon.jquery.ListRange;
import javacommon.util.StringTool;

import phapp.model.barapp.Bars;
import phapp.model.system.Role;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.ModelDriven;
import static javacommon.util.Struts2JsonHelper.*;

import javacommon.base.*;

import phapp.model.system.*;
import phapp.dao.system.*;
import phapp.service.system.impl.*;
import phapp.service.system.*;

/**
 * Title: ts_role_info Description: 接收前面界面请求,通过json数据格式交互 Action Description:
 * 页面通过Ajax调用Action层,返回JSONO数据 Copyright: Copyright (c) 2012-10 Company:
 * 
 * @author you name
 * @version 1.0
 */

public class RoleAction extends BaseStruts2Action
{

   private RoleManager roleManager;
   private TsUserRoleManager tsUserRoleManager;
   /** 通过spring自动注入 */
   public void setRoleManager(RoleManager manager)
   {
      this.roleManager = manager;
   }
   public void setTsUserRoleManager(TsUserRoleManager manager)
   {
      this.tsUserRoleManager = manager;
   }
   /**
    * jqGrid使用列表 分页查询
    * 
    * @throws IOException
    * @author
    */
   public void list() throws IOException
   {
      PageRequest<Map> pr = JqueryPageHelper.createPageRequestForJquery(getRequest());
      Page page = roleManager.findByPageRequest(pr);

      List<Role> Rolelist = (List) page.getResult();
      ListRange<Role> resultList = new ListRange<Role>();
      resultList.setDataRows(Rolelist);
      resultList.setRecords(page.getTotalCount());
      resultList.setRows(page.getPageSize());
      if (page.getTotalCount() % page.getPageSize() > 0)
      {
         resultList.setTotal(page.getTotalCount() / page.getPageSize() + 1);
      }
      else
      {
         resultList.setTotal(page.getTotalCount() / page.getPageSize());
      }
      resultList.setPage(pr.getPageNumber());
      outJson(resultList);
   }

   /**
    * Role添加
    * 
    * @author
    * @throws IOException
    */
   public void save() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();
      try
      {
         HttpServletRequest request = getRequest();
         String roleJson = request.getParameter("roleJson");
         if (null != roleJson && !"".equals(roleJson))
         {
            JSONObject jsonobject = JSONObject.fromObject(roleJson);
            Role roleobj = (Role) JSONObject.toBean(jsonobject, Role.class);
            roleManager.save(roleobj);
            result.put("success", true);
            result.put("msg", "添加成功!");
         }
         else
         {
            result.put("success", false);
            result.put("msg", "添加失败 数据错误!");
         }
      }
      catch (Exception e)
      {
         result.put("success", false);
         result.put("msg", "添加失败!");
         e.printStackTrace();
      }
      outJson(result);
   }

   /**
    * Role修改
    * 
    * @throws IOException
    * @author
    */
   public void update() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();
      try
      {
         HttpServletRequest request = getRequest();
         String roleJson = request.getParameter("roleJson");
         if (null != roleJson && !"".equals(roleJson))
         {
            JSONObject jsonobject = JSONObject.fromObject(roleJson);
            Role roleobj = (Role) JSONObject.toBean(jsonobject, Role.class);

            /** 设置主键值 */
            // roleobj.setId(jsonobject.get("id").toString());
            roleobj.setId(new Integer(jsonobject.get("roleId").toString()));
            roleobj.setRoleId(new Integer(jsonobject.get("roleId").toString()));
            roleManager.update(roleobj);
            result.put("success", true);
            result.put("msg", "修改成功!");
         }
         else
         {
            result.put("success", false);
            result.put("msg", "修改失败 数据错误!");
         }
      }
      catch (Exception e)
      {
         result.put("success", false);
         result.put("msg", "修改失败!");
         e.printStackTrace();
      }
      outJson(result);
   }
	/**
	 *  <p>标题：下拉框所用 列表 
	 * @throws IOException
	 */
	public void getComboBox() throws IOException { 
		Map<String, Object> result = new HashMap<String, Object>(); 
		try {
	         HttpServletRequest request = getRequest();
	         String userId = null==request.getParameter("userId") ? "1" : request.getParameter("userId") ;
			String str = "";
			Role barsobj = new Role();
			List list1 = roleManager.findAll();
			Map<String, Object> paramMap = new HashMap<String, Object>(); 
			paramMap.put("userId", userId);
			List list2 = tsUserRoleManager.findList(paramMap);
			List list3 = new ArrayList();
			for(int i=0;i<list1.size();i++){
				Role role = (Role)list1.get(i);
				role.setSelected(false);
				for(int j=0;j<list2.size();j++){
					TsUserRole tsUserRole = (TsUserRole)list2.get(j);
					if(role.getRoleId()==tsUserRole.getRoleId()){
						role.setSelected(true);
					}
				}
				list3.add(role);
			}
	        result.put("success", false);
	        
	        result.put("list", list3);
		} catch (Exception e) {
			e.printStackTrace();
		}
		outJson(result);
	}
   /**
    * Role删除
    * 
    * @throws IOException
    * @author
    */
   public void delete() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();
      try
      {
         HttpServletRequest request = getRequest();
         String ids = request.getParameter("ids");
         if (null != ids && !"".equals(ids))
         {
            Role role = new Role();
            role.setIdList(StringTool.stringToArray(ids));
            roleManager.removeByIdArry(role);
            result.put("success", true);
            result.put("msg", "删除成功");
         }
         else
         {
            result.put("success", false);
            result.put("msg", "删除失败 数据错误!");
         }
      }
      catch (Exception e)
      {
         result.put("success", false);
         result.put("msg", "删除失败!");
         e.printStackTrace();
      }
      outJson(result);
   }

}
